package com.zzq.my.jetpacks.jectpackapp.ui.act

import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import by.kirich1409.viewbindingdelegate.viewBinding
import com.aisier.architecture.base.BaseActivity
import com.zzq.my.jetpacks.jectpackapp.R
import com.zzq.my.jetpacks.jectpackapp.databinding.Activity12Binding


/**
 * 描述：ViewPager + FragmentPagerAdapter
 * 作者：zhuangzeqin
 * 时间: 2022/1/18-17:04
 * 邮箱：zzq@eeepay.cn
 * 备注:
 */
class EdtiextAct : BaseActivity(R.layout.activity_12) {
    private val vb by viewBinding(Activity12Binding::bind)



    /**
     * 初始化数据
     */
    override fun initData() {
        vb.btnPhone.setOnClickListener {
            vb.etPhone.setText("15019437321")
        }

    }

//    public static void onTextChanged344(EditText view, String text) {
//        if (view== null || text == null || text.length() == 0) return;
//        char space = " ";
//        int indexSpace1 = 3;
//        int indexSpace2 = 8;
//        StringBuilder sb = new StringBuilder();
////1.取出所有字符，去掉’ '和非法字符
//        for (int i = 0; i < text.length(); i++) {
////如果数字数大于11位，去掉后面的数字
//            if(sb.length() >= 11){
//                break;
//            }
////是否合法字符(0~9)
//            Pattern pattern = Pattern.compile("^[0-9]*$");
//            Matcher matcher = pattern.matcher(String.valueOf(text.charAt(i)));
//            if (text.charAt(i) != space && matcher.matches()) {
//                sb.append(text.charAt(i));
//            }
//        }
//        //2.根据长度追加' '
//        if(sb.length() > indexSpace1){
//            sb.insert(indexSpace1, space);
//        }
//        if(sb.length() > indexSpace2){
//            sb.insert(indexSpace2, space);
//        }
//        //3.设置文本和光标位置
//        if(!sb.toString().equals(text)){
//            view.setText(sb.toString());
//            view.setSelection(sb.length());
//        }
//    }

    /**
     * 初始化操作
     */
    override fun initView() {
        vb.etPhone.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                phoneTextChanged(s)
            }

            override fun afterTextChanged(s: Editable) {
                Log.d(TAG, "afterTextChanged: ${s.toString()}")
                //删除字符串中所有空格
                val phone = s.replace("\\s".toRegex(), "")
                Log.d(TAG, "afterTextChanged: $phone")
//               if (sb.length == 11) {
//                   sb.insert(3, " ");
//                   sb.insert(8, " ");
//                   setContent(sb)
//                   lastContentLength = sb.length
//               } else {
//
//               }
            }
        })


    }
    var lastContentLength = 0
    var isDelete = false
    private fun phoneTextChanged(s: CharSequence) {
        val sb = StringBuffer(s)
        //是否为输入状态
        isDelete = s.length <= lastContentLength
        //输入是第4，第9位，这时需要插入空格
        if (!isDelete && (s.length == 4 || s.length == 9)) {
            if (s.length == 4) {
                sb.insert(3, " ");
            } else {
                sb.insert(8, " ");
            }
            setContent(sb)
        }
        //删除的位置到4，9时，剔除空格
        if (isDelete && (s.length == 4 || s.length == 9)) {
            sb.deleteCharAt(sb.length - 1);
            setContent(sb)
        }
        lastContentLength = sb.length
    }

    /**
     *
     * 添加或删除空格EditText的设置
     *
     *
     *
     * @param sb
     */
    private fun setContent(sb: StringBuffer) {
        vb.etPhone.setText(sb.toString())
        //移动光标到最后面
        vb.etPhone.setSelection(sb.length)
    }

    /**
     * 初始化点击事件操作
     */
    override fun eventOnClick() {
        //实现银行卡号四位一空格展示在TextView中。
//        val cardNum = "6217710859874620".replace("\\d{4}(?!$)".toRegex(), "$0 ")
//        TextView.setText(cardNum)
    }
}